/*
 * DbAccess.h
 *
 *  Created on: Jan 28, 2013
 *      Author: root
 */

#ifndef DBACCESS_H_
#define DBACCESS_H_

#include "utils.h"
#include "smart_lock.h"
#include "sqlite3.h"
#include "Svr_log.h"
#include "SvrConfig.h"
/**
 * db connection table row  define
 *
 * sql define
 * create connecton table,and table index
 * flag:failure 0, success 1
 * status:session live AUTH_WAIT 0->1,AUTH completed 2,AUTH_DISCONN_WAIT 3->4,AUTH_DISCONN completed 5
 * */
struct connection_table_row
{
	/**
	 * coloum member
	 */
    int id;
    int Node_Id;
    int Conn_Id;
    char CleintIP[64];
    char SessionId[128];
    char File_Name[4096];
    char Url[4096];
    long long Total_KB;
    int Total_Secs;
    char CreatDateTime[64];
   	long SessionBeginTime;
   	long SessionEndTime;
	int flag;
	int status;

	/**
	 * init flag
	 */
    int fit_flag;

    connection_table_row()
    {
        id = 0;
        Node_Id = 0;
        Conn_Id = 0;
        memset(CleintIP,'\0',sizeof(CleintIP));
		memset(SessionId,'\0',sizeof(SessionId));
		memset(File_Name,'\0',sizeof(File_Name));
		memset(Url,'\0',sizeof(Url));
		Total_KB = 0;
		Total_Secs = 0;
        memset(CreatDateTime,'\0',sizeof(CreatDateTime));
        SessionBeginTime = 0;
        SessionEndTime = 0;
		flag = 0;
		status = 0;

		/**
		 * fit_flag:1,means struct data has been seted.
		 */
        fit_flag = 0;
    }
};

/**
 * file table row
 */
struct file_table_row
{
	int id;
	char FileId[32];
	char File_Name[FILE_MAX_PATH];
	char File_Name_Hash[32];
	char File_Hash[32];
	long long File_Size;
	long long CurrentFileSize;
	long CreateTime;
	long UpdateTime;
	int  Status;
	int  OpFlag;
	/**
	 * init flag
	 */
	int fit_flag;

	file_table_row()
	{
		id = 0;
		memset(FileId,'\0',sizeof(FileId));
		memset(File_Name,'\0',sizeof(File_Name));
		memset(File_Name_Hash,'\0',sizeof(File_Name_Hash));
		memset(File_Hash,'\0',sizeof(File_Hash));
		File_Size = 0;
		CurrentFileSize = 0;
		CreateTime = 0;
		UpdateTime = 0;
		Status = 0;
		OpFlag = 0;
		fit_flag = 0;
	}
};

/*
 *
 * db access class
 */
class DbAccess {
public:
	DbAccess();
	virtual ~DbAccess();

	int init(Svr_log * psvrlog , SvrConfig * pconfig);

	/**
	 *server connection  table
	 */
	int connectodb();
	int querryconnection(const char * sqlstr,connection_table_row * pRow);
	int execconnectionnosql(const char * sqlstr);

	/*
	* row buf
	*/
	int alloc_connection_row_buf(connection_table_row ** pRow);
	void reset_connection_row_buf(connection_table_row * pRow);
	void free_connection_row_buf(connection_table_row * pRow);


	/**
	 * server file table
	 */
	int openfiledb();
	int querryfileinfo(const char * sqlstr,file_table_row * pRow);
	int updatefileinfonosql(const char * sqlstr);
	int updatefilesizenosql(long long CurrentFileSize,const char *  File_Name);
	int addfileinfo(const char * FileId,
			const char *  File_Name,
			const char *  File_Name_Hash,
			const char *  File_Hash,
			long long File_Size,
			long long CurrentFileSize,
			int  Status,
			int  OpFlag);
	void reset_file_row_buf(file_table_row * pRow);

protected:
	int db_sqlite_exec(const char * sqlstr,const char *dbname);

private:
	/*
	*  /path/name.db:sqlite database file
	* */
	char connectdb[FILE_MAX_PATH];
	char filedb[FILE_MAX_PATH];
	int open_flag;

	/*lock*/
	smart_lock connectdblock;
	smart_lock filedblock;
	/*
	* table buf
	*/
	stack<connection_table_row * > free_table_pool;
	smart_lock table_pool_lock;

	/**
	* svr log
	*/
	Svr_log    * pSvrlog;
	SvrConfig  * pSvrConfig;
};

#endif /* DBACCESS_H_ */
